home *** CD-ROM | disk | FTP | other *** search
/ Power Hacker 2003 / Power_Hacker_2003.iso / Exploit and vulnerability / w00w00 / sectools / fragrouter / Libnet-0.99b / test / UDP / udp.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-07-26  |  4.2 KB  |  149 lines

  1. /*
  2.  *  $Id: udp.c,v 1.1.1.1 1999/05/18 15:33:43 dugsong Exp $
  3.  *
  4.  *  libnet
  5.  *  UDP Packet assembly tester
  6.  *
  7.  *  Copyright (c) 1998, 1999 Mike D. Schiffman <mike@infonexus.com>
  8.  *                           route|daemon9 <route@infonexus.com>
  9.  *  All rights reserved.
  10.  *
  11.  * Redistribution and use in source and binary forms, with or without
  12.  * modification, are permitted provided that the following conditions
  13.  * are met:
  14.  * 1. Redistributions of source code must retain the above copyright
  15.  *    notice, this list of conditions and the following disclaimer.
  16.  * 2. Redistributions in binary form must reproduce the above copyright
  17.  *    notice, this list of conditions and the following disclaimer in the
  18.  *    documentation and/or other materials provided with the distribution.
  19.  *
  20.  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  21.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  22.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  23.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  24.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  25.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  26.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  27.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  28.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  29.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  30.  * SUCH DAMAGE.
  31.  *
  32.  */
  33.  
  34. #if (HAVE_CONFIG_H)
  35. #include "../../include/config.h"
  36. #endif
  37. #include "../libnet_test.h"
  38.  
  39. int
  40. main(int argc, char **argv)
  41. {
  42.     int sock, c;
  43.     u_long src_ip, dst_ip;
  44.     u_short src_prt, dst_prt;
  45.     u_char *cp, *buf;
  46.  
  47.     printf("UDP packet building/writing test\n");
  48.  
  49.     src_ip  = 0;
  50.     dst_ip  = 0;
  51.     src_prt = 0;
  52.     dst_prt = 0;
  53.     while((c = getopt(argc, argv, "d:s:")) != EOF)
  54.     {
  55.         switch (c)
  56.         {
  57.             /*
  58.              *  We expect the input to be of the form `ip.ip.ip.ip.port`.  We
  59.              *  point cp to the last dot of the IP address/port string and
  60.              *  then seperate them with a NULL byte.  The optarg now points to
  61.              *  just the IP address, and cp points to the port.
  62.              */
  63.             case 'd':
  64.                 if (!(cp = strrchr(optarg, '.')))
  65.                 {
  66.                     usage(argv[0]);
  67.                 }
  68.                 *cp++ = 0;
  69.                 dst_prt = (u_short)atoi(cp);
  70.                 if (!(dst_ip = name_resolve(optarg, 1)))
  71.                 {
  72.                     fprintf(stderr, "Bad destination IP address: %s\n", optarg);
  73.                     exit(1);
  74.                 }
  75.                 break;
  76.             case 's':
  77.                 if (!(cp = strrchr(optarg, '.')))
  78.                 {
  79.                     usage(argv[0]);
  80.                 }
  81.                 *cp++ = 0;
  82.                 src_prt = (u_short)atoi(cp);
  83.                 if (!(src_ip = name_resolve(optarg, 1)))
  84.                 {
  85.                     fprintf(stderr, "Bad source IP address: %s\n", optarg);
  86.                     exit(1);
  87.                 }
  88.                 break;
  89.         }
  90.     }
  91.     if (!src_ip || !src_prt || !dst_ip || !dst_prt)
  92.     {
  93.         usage(argv[0]);
  94.         exit(EXIT_FAILURE);
  95.     }
  96.  
  97.     buf = malloc(UDP_H + IP_H);
  98.     if (!buf)
  99.     {
  100.         perror("No memory for packet header");
  101.         exit(EXIT_FAILURE);
  102.     }
  103.  
  104.     sock = open_raw_sock(IPPROTO_RAW);
  105.     if (sock == -1)
  106.     {
  107.         perror("No socket");
  108.         exit(EXIT_FAILURE);
  109.     }
  110.     
  111.     build_ip(UDP_H,
  112.             0,
  113.             242,
  114.             0,
  115.             64,
  116.             IPPROTO_UDP,
  117.             src_ip,
  118.             dst_ip,
  119.             NULL,
  120.             0,
  121.             buf);
  122.  
  123.     build_udp(src_prt, dst_prt, NULL, 0, buf + IP_H);
  124.  
  125.     do_checksum(buf, IPPROTO_UDP, UDP_H);
  126.  
  127.     c = write_ip(sock, buf, UDP_H + IP_H);
  128.     if (c < UDP_H + IP_H)
  129.     {
  130.         fprintf(stderr, "write_ip\n");
  131.     }
  132.     printf("Completed, wrote %d bytes\n", c);
  133.  
  134.     free(buf);
  135.  
  136.     return (c == -1 ? EXIT_FAILURE : EXIT_SUCCESS);
  137. }
  138.  
  139.  
  140. void
  141. usage(u_char *name)
  142. {
  143.     fprintf(stderr,
  144.         "usage: %s -s source_ip:source_port -d destination_ip:destination_port\n",
  145.         name);
  146. }
  147.  
  148. /* EOF */
  149.